package com.EM1.www.dao;

import com.EM1.www.po.Student;
import com.EM1.www.util.Connectmysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class ConnectGrade {
    public ArrayList<Student> selcetGrade(int grade) throws Exception {
        System.out.println(grade);
        Connection conn = Connectmysql.Connectmysql();
        String sql="select  username,intruduction from student where grade=?;";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setInt(1,grade);
        ResultSet res = pmt.executeQuery();
        ArrayList<Student> students=new ArrayList<>();
        while (res.next()){
            Student student=new Student(grade,res.getString("intruduction"));
            student.setName(res.getString("username"));
            students.add(student);
        }
        pmt.close();
        conn.close();
        return students;
    }
    public ArrayList<Integer> selectAllgrade() throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        PreparedStatement pmt = conn.prepareStatement("select *from grade");
        ResultSet res = pmt.executeQuery();
        ArrayList<Integer> grades=new ArrayList<>();
        while (res.next()){
            grades.add(res.getInt("id"));
        }
        return grades;
    }
    public void insertGrade(int teacherId,int gradeId) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        PreparedStatement pmt = conn.prepareStatement("insert into teacher_grade(grade_id,teacher_id) values (?,?)");
        pmt.setInt(1,gradeId);
        pmt.setInt(2,teacherId);
        pmt.executeUpdate();
        pmt.close();
    }

}
